/*
   Project AwesOmeScript
   Copyright 2010 Domenico Gentner

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "stdafx.hpp"
#include "Viewbase.hpp"
#include "HtmlEditSettings.hpp"
#include "StyleDescriptor.hpp"
#include "SettingsDialog.hpp"


BEGIN_EVENT_TABLE(SettingsDialog, wxDialog)
	EVT_BUTTON(idSETTINGS_RESET_TO_STANDARD, SettingsDialog::ResetToStandard)
END_EVENT_TABLE()


SettingsDialog::SettingsDialog( wxWindow* parent, HtmlEdit* ctrl )
: wxDialog(parent, wxID_ANY, _("Settings")), m_edit_ctrl(ctrl)
{
	HtmlEditSettings s;
	HtmlEditSettings::TStyleList style = s.GetStyleList();

	wxGridSizer* gSizer = new wxGridSizer(7, 1, 0, 0);

	//////////////////////////////////////////////////////////////////////////
	// wxSTC_H_DEFAULT
	wxStaticBoxSizer* sb_defaultSizer = new wxStaticBoxSizer(new wxStaticBox( this, wxID_ANY, _(style[wxSTC_H_DEFAULT]->GetDescription())), wxHORIZONTAL);
	m_pick_default_foreground = new wxColourPickerCtrl(this, wxID_ANY, style[wxSTC_H_DEFAULT]->GetForeground() );
	m_pick_default_background = new wxColourPickerCtrl(this, wxID_ANY, style[wxSTC_H_DEFAULT]->GetBackground() );

	{
		wxStaticText* lblForeground = new wxStaticText(this, wxID_ANY, _("Foreground:"));
		wxStaticText* lblBackGround = new wxStaticText(this, wxID_ANY, _("Background:"));
		sb_defaultSizer->Add(lblForeground, 1, wxALL, 5);
		sb_defaultSizer->Add(m_pick_default_foreground, 1, wxALL, 5);
		sb_defaultSizer->Add(lblBackGround, 1, wxALL, 5);
		sb_defaultSizer->Add(m_pick_default_background, 1, wxALL, 5);
	}	

	//////////////////////////////////////////////////////////////////////////
	// wxSTC_H_TAG
	wxStaticBoxSizer* sb_tagSizer = new wxStaticBoxSizer(new wxStaticBox( this, wxID_ANY, _(style[wxSTC_H_TAG]->GetDescription())), wxHORIZONTAL);
	m_pick_tag_foreground = new wxColourPickerCtrl(this, wxID_ANY, style[wxSTC_H_TAG]->GetForeground() );
	m_pick_tag_background = new wxColourPickerCtrl(this, wxID_ANY, style[wxSTC_H_TAG]->GetBackground() );

	{
		wxStaticText* lblForeground = new wxStaticText(this, wxID_ANY, _("Foreground:"));
		wxStaticText* lblBackGround = new wxStaticText(this, wxID_ANY, _("Background:"));
		sb_tagSizer->Add(lblForeground, 1, wxALL, 5);
		sb_tagSizer->Add(m_pick_tag_foreground, 1, wxALL, 5);
		sb_tagSizer->Add(lblBackGround, 1, wxALL, 5);
		sb_tagSizer->Add(m_pick_tag_background, 1, wxALL, 5);
	}	

	//////////////////////////////////////////////////////////////////////////
	// wxSTC_H_ATTRIBUTE
	wxStaticBoxSizer* sb_attribSizer = new wxStaticBoxSizer(new wxStaticBox( this, wxID_ANY, _(style[wxSTC_H_ATTRIBUTE]->GetDescription())), wxHORIZONTAL);
	m_pick_attrib_foreground = new wxColourPickerCtrl(this, wxID_ANY, style[wxSTC_H_ATTRIBUTE]->GetForeground() );
	m_pick_attrib_background = new wxColourPickerCtrl(this, wxID_ANY, style[wxSTC_H_ATTRIBUTE]->GetBackground() );

	{
		wxStaticText* lblForeground = new wxStaticText(this, wxID_ANY, _("Foreground:"));
		wxStaticText* lblBackGround = new wxStaticText(this, wxID_ANY, _("Background:"));
		sb_attribSizer->Add(lblForeground, 1, wxALL, 5);
		sb_attribSizer->Add(m_pick_attrib_foreground, 1, wxALL, 5);
		sb_attribSizer->Add(lblBackGround, 1, wxALL, 5);
		sb_attribSizer->Add(m_pick_attrib_background, 1, wxALL, 5);
	}		

	//////////////////////////////////////////////////////////////////////////
	// wxSTC_H_SINGLESTRING
	wxStaticBoxSizer* sb_stringSizer = new wxStaticBoxSizer(new wxStaticBox( this, wxID_ANY, _(style[wxSTC_H_SINGLESTRING]->GetDescription())), wxHORIZONTAL);
	m_pick_string_foreground = new wxColourPickerCtrl(this, wxID_ANY, style[wxSTC_H_SINGLESTRING]->GetForeground() );
	m_pick_string_background = new wxColourPickerCtrl(this, wxID_ANY, style[wxSTC_H_SINGLESTRING]->GetBackground() );

	{
		wxStaticText* lblForeground = new wxStaticText(this, wxID_ANY, _("Foreground:"));
		wxStaticText* lblBackGround = new wxStaticText(this, wxID_ANY, _("Background:"));
		sb_stringSizer->Add(lblForeground, 1, wxALL, 5);
		sb_stringSizer->Add(m_pick_string_foreground, 1, wxALL, 5);
		sb_stringSizer->Add(lblBackGround, 1, wxALL, 5);
		sb_stringSizer->Add(m_pick_string_background, 1, wxALL, 5);
	}

	//////////////////////////////////////////////////////////////////////////
	// wxSTC_H_NUMBER
	wxStaticBoxSizer* sb_numberSizer = new wxStaticBoxSizer(new wxStaticBox( this, wxID_ANY, _(style[wxSTC_H_NUMBER]->GetDescription())), wxHORIZONTAL);
	m_pick_number_foreground = new wxColourPickerCtrl(this, wxID_ANY, style[wxSTC_H_NUMBER]->GetForeground() );
	m_pick_number_background = new wxColourPickerCtrl(this, wxID_ANY, style[wxSTC_H_NUMBER]->GetBackground() );

	{
		wxStaticText* lblForeground = new wxStaticText(this, wxID_ANY, _("Foreground:"));
		wxStaticText* lblBackGround = new wxStaticText(this, wxID_ANY, _("Background:"));
		sb_numberSizer->Add(lblForeground, 1, wxALL, 5);
		sb_numberSizer->Add(m_pick_number_foreground, 1, wxALL, 5);
		sb_numberSizer->Add(lblBackGround, 1, wxALL, 5);
		sb_numberSizer->Add(m_pick_number_background, 1, wxALL, 5);
	}

	wxBoxSizer* button_sizer = new wxBoxSizer(wxHORIZONTAL);
	wxButton* reset_button = new wxButton(this, idSETTINGS_RESET_TO_STANDARD, _("Restore Defaults"));
	button_sizer->Add(reset_button, wxALL|wxEXPAND, 5);	

	//////////////////////////////////////////////////////////////////////////
	// Create Buttons
	wxStdDialogButtonSizer* btns = new wxStdDialogButtonSizer;		
	btns->AddButton(new wxButton(this, wxID_OK, _("Ok")));
	btns->AddButton(new wxButton(this, wxID_CANCEL, _("Cancel")));
	btns->Realize();
	

	// Add to global sizer
	gSizer->Add(sb_defaultSizer, 1, wxALL|wxSHAPED, 5);
	gSizer->Add(sb_tagSizer, 1, wxALL|wxSHAPED , 5);
	gSizer->Add(sb_attribSizer, 1, wxALL|wxSHAPED , 5);
	gSizer->Add(sb_stringSizer, 1, wxALL|wxSHAPED , 5);
	gSizer->Add(sb_numberSizer, 1, wxALL|wxSHAPED, 5);
	gSizer->Add(button_sizer, 1, wxEXPAND, 5);
	gSizer->Add(btns, 1, wxALL|wxSHAPED, 0);

	this->SetSizerAndFit(gSizer);
	this->CenterOnParent();
}

void SettingsDialog::Save()
{
	HtmlEditSettings s;
	HtmlEditSettings::TStyleList style = s.GetStyleList();

	style[wxSTC_H_DEFAULT]->SetForeground(m_pick_default_foreground->GetColour());
	style[wxSTC_H_DEFAULT]->SetBackground(m_pick_default_background->GetColour());

	style[wxSTC_H_ATTRIBUTE]->SetForeground(m_pick_attrib_foreground->GetColour());
	style[wxSTC_H_ATTRIBUTE]->SetBackground(m_pick_attrib_background->GetColour());

	style[wxSTC_H_TAG]->SetForeground(m_pick_tag_foreground->GetColour());
	style[wxSTC_H_TAG]->SetBackground(m_pick_tag_background->GetColour());

	style[wxSTC_H_SINGLESTRING]->SetForeground(m_pick_string_foreground->GetColour());
	style[wxSTC_H_SINGLESTRING]->SetBackground(m_pick_string_background->GetColour());

	style[wxSTC_H_NUMBER]->SetForeground(m_pick_number_foreground->GetColour());
	style[wxSTC_H_NUMBER]->SetBackground(m_pick_number_background->GetColour());

	s.WriteSettings();
	m_edit_ctrl->SetMyStyle();
}

void SettingsDialog::ResetToStandard( wxCommandEvent& )
{
	HtmlEditSettings s;
	s.ResetToStandard();
	HtmlEditSettings::TStyleList style = s.GetStyleList();	

	m_pick_default_foreground->SetColour(style[wxSTC_H_DEFAULT]->GetForeground());
	m_pick_default_background->SetColour(style[wxSTC_H_DEFAULT]->GetBackground());

	m_pick_tag_foreground->SetColour(style[wxSTC_H_TAG]->GetForeground());
	m_pick_tag_background->SetColour(style[wxSTC_H_TAG]->GetBackground());

	m_pick_attrib_foreground->SetColour(style[wxSTC_H_ATTRIBUTE]->GetForeground());
	m_pick_attrib_background->SetColour(style[wxSTC_H_ATTRIBUTE]->GetBackground());

	m_pick_string_foreground->SetColour(style[wxSTC_H_SINGLESTRING]->GetForeground());
	m_pick_string_background->SetColour(style[wxSTC_H_SINGLESTRING]->GetBackground());

	m_pick_number_foreground->SetColour(style[wxSTC_H_NUMBER]->GetForeground());
	m_pick_number_background->SetColour(style[wxSTC_H_NUMBER]->GetBackground());

	s.WriteSettings();
	m_edit_ctrl->SetMyStyle();
}